package chapter06._01_InterfaceTest;

import java.util.Comparator;

/**
 * @Version:
 * @Description:接口练习
 * @LastDate:2020年9月24日下午10:43:47
 */
public class InterfaceTest implements Test1, Test { // can implement multiple interfaces
	
	/*
	 * Conflict of default methods
	 * if the class extends class, implements multiple interface 
	 * and the, have the default method of same name 
	 * and one of them implement the default method
	 * it will report a error
	 * 
	 * the rules of solving the problem
	 * 1.the super class first
	 * 2.if not extend class, Java leaves the problem to coder to fix up
	 *   coder have to provide a specific method
	 * 
	 * in this case, we can choose the one of methods
	 * 
	 * it's not important how it conflicts
	 * if at least one of interfaces provide the implementation, it will report
	 * if all interfaces do not provide an implementation for the method, there's no conflict here.
	 * so you can implement it or just leave it which means the class is abstract
	 */
	public int zero() {
		return Test.super.zero();
	}
	
	public static void main(String[] args) {
		//Toolkit.getDefaultToolkit().beep();
	}

}

interface Test {
	int value = 100; //can't definite field,but constant, default public static final constant
	
	//allow create static method and private method
	//but the use of private method is limited, because it just can be used in the interface itself 
	static int min(int a, int b) {
		return a > b ? a : b;
	}
	
	/* can implement default method in interface 
	 * most of the time, default method
	 * will be overwrite but sometimes, it works wonders
	 * the benefits of default method is, it can fix two problems
	 * p1.program can't build because add a not default method
	 * p2.call a not default method on a instance of interface
	 */
	default int zero() {
		//can call other method
		return min(1, 0);
	}
}
interface Test1 {
	int zero();
}
//interface can extend other interface
interface Test2 extends Test {
	
}
//re-implement the comparator 
class SortByLength implements Comparator<String>,Comparable<Integer> {
	int id;
	
	//this is the Comparable
	//example: List<SortByLength> list = new ArrayList<>(); Collections.sort(list);
	@Override
	public int compareTo(Integer o) {
		return id - o.intValue();
	}
	
	//this is the Comparator
	//example: Arrays.sort(arr, new SortLength());
	public int compare(String s1, String s2) {
		return s1.length() - s2.length();
	}
}